package com.ruoyi.iot.domain.emun;

import org.apache.iotdb.isession.pool.SessionDataSetWrapper;
import org.apache.iotdb.session.pool.SessionPool;

import com.ruoyi.iotcommon.utils.IotdbSqlUtils;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;

import java.util.Random;

@Slf4j
public class IotdbTest {
    

    @Resource
    SessionPool sessionPool;

    String productKey = "abc";

    String deviceKey = "d123";

    String channelKey = "property";

    String propertyKey = "propertyKey1";

    Random random = new Random();


    public void testLast() {

       String tableName = IotdbSqlUtils.buildTableName(productKey, channelKey, deviceKey);
   
       String sql = IotdbSqlUtils.selectLast(tableName);
       try (SessionDataSetWrapper wrapper = sessionPool.executeQueryStatement(sql)){
        while (wrapper.hasNext()) {
            log.info("testLast-设备数据：{}",wrapper.next());
          }
       } catch (Exception e) {
            log.error(e.getMessage());
       }
    }


    public void testPage() {
        String tableName = IotdbSqlUtils.buildTableName(productKey, channelKey, deviceKey);

        String sql = IotdbSqlUtils.selectPage(tableName,1,10,1744469950000L, System.currentTimeMillis());
        try (SessionDataSetWrapper wrapper = sessionPool.executeQueryStatement(sql)){
            while (wrapper.hasNext()) {
                log.info("testPage-设备数据：{}",wrapper.next());
            }
        } catch (Exception e) {
            log.error(e.getMessage());
        }
    }


    public void testCount() {
        String tableName = IotdbSqlUtils.buildTableName(productKey, channelKey, deviceKey);

        String sql = IotdbSqlUtils.selectCount(tableName,1744469950000L, System.currentTimeMillis());
        try (SessionDataSetWrapper wrapper = sessionPool.executeQueryStatement(sql)){
            while (wrapper.hasNext()) {
                log.info("testCount-数量统计：{}",wrapper.next());
            }
        } catch (Exception e) {
            log.error(e.getMessage());
        }
    }


    public void testList() {
        String tableName = IotdbSqlUtils.buildTableName(productKey, channelKey, deviceKey);

        String sql = IotdbSqlUtils.selectList(tableName,1744469950000L, System.currentTimeMillis(), 10);
        try (SessionDataSetWrapper wrapper = sessionPool.executeQueryStatement(sql)){
            while (wrapper.hasNext()) {
                log.info("testList-设备数据：{}",wrapper.next());
            }
        } catch (Exception e) {
            log.error(e.getMessage());
        }
    }

    public void testInsert() {
        String tableName = IotdbSqlUtils.buildTableName(productKey, channelKey, deviceKey);
        for (int i = 0; i < 5; i++) {
            try {
                String sql = "INSERT INTO " + tableName + "(propertyKey1,propertyKey2,propertyKey3) VALUES ("+random.nextDouble()+", "+random.nextDouble()+","+random.nextDouble()+")";
                sessionPool.executeNonQueryStatement(sql);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }

    }
}
